package com.loft.controller.baseview;

import com.loft.R;
import com.loft.comm.util.CloseCenter;
import com.loft.comm.util.Utility;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.InflateException;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.LayoutInflater.Factory;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

/**
 * @ClassName: BaseActivity.java
 * @Description:���н���ĸ���
 * @Author: heb
 * @CreateDate: 2012/03/28
 * @Version: v1.0.0
 * 
 */

public class BaseActivity extends Activity implements OnClickListener {

	private Toast _toast;
	private CloseCenter _closeCenter;
	private Dialog _dialog;
	public Dialog get_dialog() {
		return _dialog;
	}

	public void set_dialog(Dialog dialog) {
		_dialog = dialog;
	}

	public final int DIALOG_DEFAULT = 0;
	public final int DIALOG_LOADING = 1;
	public final int DIALOG_SUBMIT = 2;
	public final int DIALOG_UPDATE = 3;
	public final int DIALOG_DELETE = 4;
	public final int DIALOG_EXIT = 5;
	public int _width, _heigth;
	Utility _utility;
	// private Utility utility;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		_width = getWindowManager().getDefaultDisplay().getWidth();
		_heigth = getWindowManager().getDefaultDisplay().getHeight();
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		_utility = new Utility(getApplication());
		_closeCenter = CloseCenter.getInstance();
		_closeCenter.add(this);
		init();
	}

	@Override
	public void finish() {
		_closeCenter.remove(this);
		super.finish();
	}

	/**
	 * @MethodName: onCreateDialog
	 * @Description:�����Զ���dialog
	 * @Author: heb
	 * @param id
	 *            :��ݲ�ͬid������ͬĿ��dialog
	 * @CreateDate: 2012/03/28
	 */
	@Override
	protected Dialog onCreateDialog(int dialogKindId) {
		if (_dialog != null) {
			_dialog.cancel();
		}
		RotateAnimation rotateAnimation = new RotateAnimation(0, -360, 75, 75);
		rotateAnimation.setDuration(600);
		rotateAnimation.setRepeatCount(Animation.INFINITE);
		rotateAnimation.setRepeatMode(Animation.RESTART);
		_dialog = new Dialog(this, R.style.dialog);
		View view = getLayoutInflater().inflate(R.layout.dialog, null);
		_dialog.setContentView(view, new LayoutParams(_width - 100,
				_width - 100));
		ImageView dialog_circle_imageView = (ImageView) view
				.findViewById(R.id.dialog_circle);
		TextView dialog_msg_textView = (TextView) view
				.findViewById(R.id.dialog_msg);
		switch (dialogKindId) {
		case DIALOG_DEFAULT:
			dialog_circle_imageView.startAnimation(rotateAnimation);
			dialog_msg_textView.setText(R.string.dialog_dedault);
			break;
		case DIALOG_DELETE:
			dialog_circle_imageView.startAnimation(rotateAnimation);
			dialog_msg_textView.setText(R.string.dialog_delete);
			break;
		case DIALOG_LOADING:
			dialog_circle_imageView.startAnimation(rotateAnimation);
			dialog_msg_textView.setText(R.string.dialog_loading);
			break;
		case DIALOG_SUBMIT:
			dialog_circle_imageView.startAnimation(rotateAnimation);
			dialog_msg_textView.setText(R.string.dialog_submit);
			break;
		case DIALOG_UPDATE:
			dialog_circle_imageView.startAnimation(rotateAnimation);
			dialog_msg_textView.setText(R.string.dialog_update);
			break;
		case DIALOG_EXIT:
			dialog_circle_imageView.startAnimation(rotateAnimation);
			dialog_msg_textView.setText(R.string.dialog_exit);
			break;
		default:
			break;
		}
		return _dialog;
	}

	/**
	 * @MethodName: createToast
	 * @Description:����Toast
	 * @Author: heb
	 * @CreateDate: 2012/03/28
	 */
	public void createToast() {
		if (_toast != null)
			_toast.cancel();
		_toast = new Toast(this);
	}

	/**
	 * @MethodName: showToast
	 * @Description:��ʾToast
	 * @Author: heb
	 * @param : toastTextId (toastMsg ID)
	 * @CreateDate: 2012/03/28
	 */
	public void showToast(int toastMsgId) {
		createToast();
		_toast.setDuration(Toast.LENGTH_SHORT);
		_toast.setText(toastMsgId);
		_toast.show();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// to
		// MenuInflater _menuInflater = new MenuInflater(this);
		// menuInflater.inflate(R.menu.comm_menu, menu);
		setMenuBackground();
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		// to do
		}
		return true;
	}

	/**
	 * @MethodName: setMenuBackground
	 * @Description:����menu�˵�����
	 * @Author: heb
	 * @CreateDate: 2012/03/28
	 */
	private void setMenuBackground() {
		getLayoutInflater().setFactory(new Factory() {
			@Override
			public View onCreateView(String name, Context context,
					AttributeSet attrs) {
				if (name
						.equalsIgnoreCase("com.android.internal.view.menu.IconMenuItemView")) {
					try {
						LayoutInflater f = getLayoutInflater();
						final View view = f.createView(name, null, attrs);
						new Handler().post(new Runnable() {
							public void run() {
								// view
								// .setBackgroundResource(R.drawable.comm_menu_bg);
							}
						});
						return view;
					} catch (InflateException e1) {

					} catch (ClassNotFoundException e) {

					} finally {

					}
				}
				return null;
			}
		});
	}

	/**
	 * @MethodName: exitApp
	 * @Description:�˳�Ӧ�ó���
	 * @Author: heb
	 * @CreateDate: 2012/03/28
	 */
	protected void exitApp() {
		// AlertDialog alertDialog = new AlertDialog.Builder(this).setTitle(
		// R.string.system_exit_application_title_str).setIcon(
		// R.drawable.icon).setMessage(
		// R.string.system_exit_application_msg_str).setNegativeButton(
		// R.string.system_exit_application_no_str, null)
		// .setPositiveButton(R.string.system_exit_application_yes_str,
		// new DialogInterface.OnClickListener() {
		// @Override
		// public void onClick(DialogInterface dialog,
		// int which) {
		// ActivityManager activityManager = (ActivityManager)
		// getSystemService(Context.ACTIVITY_SERVICE);
		// activityManager
		// .restartPackage(getPackageName());
		// if (CameraManager.get() != null) {
		// CameraManager.get().closeDriver();
		// }
		// _closeCenter.Close();
		// _closeCenter.CloseHome();
		// }
		// }).create();
		// alertDialog.show();
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
	}

	/**
	 * @MethodName: initView
	 * @Description:����findviewbyId�������ʼ������
	 * @Author: heb
	 * @CreateDate: 2012/04/23
	 */
	public void initView() {

	}

	/**
	 * @MethodName: setListeners
	 * @Description:��ʼ��������
	 * @Author: heb
	 * @CreateDate: 2012/04/23
	 */
	public void setListeners() {

	}

	/**
	 * @MethodName: setAdapers
	 * @Description:��ʼ��������
	 * @Author: heb
	 * @CreateDate: 2012/04/23
	 */
	public void setAdapers() {

	}

	/**
	 * @MethodName: setAdapers
	 * @Description:��ʼ��������
	 * @Author: heb
	 * @CreateDate: 2012/04/23
	 */
	public void initAnimation() {

	}

	/**
	 * @MethodName: init
	 * @Description:��ʼ��
	 * @Author: heb
	 * @CreateDate: 2012/04/23
	 */
	private void init() {
		initView();
		initAnimation();
		setAdapers();
		setListeners();
	}
}
